package com.dure.hotmemory.utils;

import android.app.Activity;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.dure.commonlib.util.StatisticUtil;
import com.dure.hotmemory.R;
import com.dure.hotmemory.listeners.AdPresentListener;
import com.qq.e.ads.banner.ADSize;
import com.qq.e.ads.banner.AbstractBannerADListener;
import com.qq.e.ads.banner.BannerView;
import com.qq.e.ads.interstitial.AbstractInterstitialADListener;
import com.qq.e.ads.interstitial.InterstitialAD;
import com.qq.e.ads.interstitial.InterstitialADListener;
import com.qq.e.ads.splash.SplashAD;
import com.qq.e.ads.splash.SplashADListener;
import com.qq.e.comm.util.AdError;
import com.socks.library.KLog;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by leix on 6/28/16.
 */
public class GDTADUtil {

    public static BannerView bv;
    public static InterstitialAD  iad;
    public  static boolean interstitialADPrepared;

    // 横幅广告
    public static void showBannerView(final Activity context, RelativeLayout view) {
        bv = new BannerView(context, ADSize.BANNER,
                context.getResources().getString(R.string.gdt_app_id),
                context.getResources().getString(R.string.gdt_banner_id));
        bv.setRefresh(30);
        bv.setADListener(new AbstractBannerADListener() {

            @Override
            public void onNoAD(AdError adError) {
                KLog.i("lesleixz", "BannerNoAD，eCode=" + adError);
            }

            @Override
            public void onADReceiv() {
                KLog.i("lesleixz", "ONBannerReceive");
            }

            @Override
            public void onADClicked() {
                super.onADClicked();
                MobclickAgent.onEvent(context, "banner_click");
            }

            @Override
            public void onADExposure() {
                super.onADExposure();
                MobclickAgent.onEvent(context, "banner_present");
            }
        });
        view.addView(bv);
        bv.loadAD();
    }

    public static void destroyBannerAd() {
        if (bv != null) {
//            view.removeAllViews();
            bv.destroy();
            bv = null;
        }
    }


    public static void showSplashAd(final Activity context, RelativeLayout view, final AdPresentListener listener){
        SplashAD splashAD =new SplashAD(context, view, null, context.getString(R.string.gdt_app_id), context.getString(R.string.gdt_splash_id),
                new SplashADListener() {
                    @Override
                    public void onADDismissed() {
                        Log.d("lesleixz", "onADDismissed");
                        if (listener != null){
                            listener.adDismiss();
                        }
                    }

                    @Override
                    public void onNoAD(AdError adError) {
                        Log.d("lesleixz", "onNoAD");
                        if (listener != null){
                            listener.adDismiss();
                        }
                    }

                    @Override
                    public void onADPresent() {
                        Log.d("lesleixz", "onADPresent");
                    }

                    @Override
                    public void onADClicked() {
                        Log.d("lesleixz", "onADClicked");
                    }

                    @Override
                    public void onADTick(long l) {
                        Log.d("lesleixz", "onADTick = " + l);
                    }

                    @Override
                    public void onADExposure() {
                        Log.d("lesleixz", "onADExposure");
                    }
                }, 0);


    }

    public static void showInterstitialAd(final Activity context){
        if (iad ==null){
            iad = new InterstitialAD(context, context.getString(R.string.gdt_app_id),context.getString(R.string.gdt_interstitial_id));

            iad.setADListener(new AbstractInterstitialADListener() {

                @Override
                public void onADReceive() {
                    Log.d("lesleixz", "showInterstitialAd onADReceive");


                    interstitialADPrepared = true;
                }

                @Override
                public void onNoAD(AdError adError) {
                    Log.d("lesleixz", "showInterstitialAd onNoAD");
                }

            });
            iad.loadAD();
        }else{
            if (interstitialADPrepared){
                iad.showAsPopupWindow();
            }
        }

//        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
//        imm.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), 0); //强制隐藏键盘
    }


}
